package imvu.rbox.object
{
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import imvu.rbox.event.ActionEvent;
	import imvu.rbox.struct.CGameRoomInfo;

	public class RoomSelect extends Sprite
	{
		public var mcHotMask:MovieClip;			//	热响应区块
		public var btnSingle:SimpleButton;		//	单人游戏模式
		public var btnCreate:SimpleButton;		//	创建一个游戏房间
		public var btnRush:SimpleButton;		//	刷新房间列表
		public var btnPrevPage:SimpleButton;	//	上一页
		public var btnNextPage:SimpleButton;	//	下一页
		
		public var room1:RoomLine;		//	第一个房间
		public var room2:RoomLine;		//	第二个房间
		public var room3:RoomLine;		//	第三个房间
		public var room4:RoomLine;		//	第四个房间
		public var room5:RoomLine;		//	第五个房间

		public var sprMask:Sprite;		//	遮照响应区块
		public var mcCreate:Sprite;		//	创建房间对象
		
		private var sprCreate:CreateNewRoom;	//	创建新房间面板
		private var roomArray:Array;	//	房间列表
		
		private var nowPage:Number = 0;	//	当前页
		
		public function RoomSelect()
		{
			InitObject();
			InitListener();
		}
		
		public function DeleteListener():void
		{
			
		}
		
		//	刷新房间列表中的信息
		public function RushRoom(array:Array):void
		{
			roomArray = array;
			
			// this.nowPage = 0;
			
			UpdataRoomList();
		}
		
		private function UpdataRoomList():void
		{			
			for(var i:int = 0; i < 5; i++)
			{
				var rID:Number = this.nowPage * 5 + i;		
				if(rID < roomArray.length)
				{
					RoomLine(this["room" + Number(i+1)]).info = roomArray[rID] as CGameRoomInfo;
				}
				else
				{
					RoomLine(this["room" + Number(i+1)]).info = null;
				}
			}
		}
		
		//	设置对象中的所有按钮是否可以点击
		public function set enabled(flag:Boolean):void
		{
			this.mcHotMask.visible = !flag;
			this.mcHotMask.gotoAndStop(1);
		}
		
		//-------------------------------------------------------------------------------
		
		private function InitObject():void
		{
			this.btnSingle.visible = false;
			this.enabled = true;
			this.roomArray = new Array();
		}
		
		private function InitListener():void
		{
			//	为房间列表设置事件侦听
			for(var i:int = 1; i <= 5; i++)
			{
				RoomLine(this["room" + i]).addEventListener(ActionEvent.JOIN_ROOM, onJoinRoomEvent);
			}
			
			//	为各个按钮设置事件侦听
			this.btnCreate.addEventListener(MouseEvent.CLICK, onCreateNewRoom);
			this.btnRush.addEventListener(MouseEvent.CLICK, onRushRoomList);
			this.btnNextPage.addEventListener(MouseEvent.CLICK, onChangeNextPage);
			this.btnPrevPage.addEventListener(MouseEvent.CLICK, onChangePrevPage);
			
			this.mcHotMask.addEventListener(MouseEvent.CLICK, function(e:*):void {});
		}
		
		//	发送 加入游戏 的消息
		private function onJoinRoomEvent(e:ActionEvent):void
		{
			var act:ActionEvent = new ActionEvent(ActionEvent.JOIN_ROOM, e.data);
			
			this.enabled = false;
			
			this.dispatchEvent(act);
		}
		
		private function onCreateNewRoom(e:MouseEvent):void
		{
			sprCreate = new CreateNewRoom();
			this.addChild(sprCreate);
			
			sprCreate.x = 0;//(sprMask.width - sprCreate.width)/2;
			sprCreate.y = 0;//(sprMask.height - sprCreate.height)/2;
			
			this.addEventListener(Event.REMOVED, onRemovedEvent);
			
			sprCreate.addEventListener(ActionEvent.CREATE_NEW_ROOM, onCreateRoomDone);
		}
		
		private function onRemovedEvent(e:Event):void
		{
			if(e.target is CreateNewRoom)
			{
				// trace("reMoveObject::" + e.target);
				sprCreate.addEventListener(ActionEvent.CREATE_NEW_ROOM, onCreateRoomDone);
			}			
		}
		
		//	创建一个新房间
		private function onCreateRoomDone(e:ActionEvent):void
		{
			var act:ActionEvent = new ActionEvent(ActionEvent.CREATE_NEW_ROOM, e.data);
									
			sprCreate.HiddenThis();

			this.dispatchEvent(act);
		}
		
		private function onRushRoomList(e:MouseEvent):void
		{
			this.nowPage = 0;
			
			UpdataRoomList();
		}
		
		private function onChangeNextPage(e:MouseEvent):void
		{
			if(this.roomArray.length > 5)
			{
				if((this.nowPage + 1) * 5 <= this.roomArray.length)
				{
					this.nowPage ++;
					
					UpdataRoomList();
				}
			}
		}
		
		private function onChangePrevPage(e:MouseEvent):void
		{
			if(this.roomArray.length > 5)
			{
				if(this.nowPage > 0)
				{
					this.nowPage --;
					UpdataRoomList();
				}	
			}
		}
	}
}